import axios from "../../utils/axios"
// import {toOpenDoor} from "../../utils/initTTL"
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    list:[]
  },
  attached(){
    this.getList();
  },
  methods:{
    getList(){
      axios.get("/app/roomList").then(res=>{
        this.setData({
          list:res.data?.map(item=>{
            const statusArr = [
              {
                text:"空闲中",
                color:"green",
                value:1
              },{
                text:"使用中",
                color:"orange",
                value:2
              },{
                text:"维护中",
                color:"red",
                value:3
              },{
                text:"待打扫",
                color:"red",
                value:4
              },
              {
                text:"保洁中",
                color:"grey",
                value:5
              },
            ];
            const cur = statusArr.find(ite=>ite.value === item.status) || {
              text:"空闲中",
              color:"green",
            }
            item.statusText = cur.text;
            item.statusColor = cur.color;
            return item;
          })
        })
      })
    },
    // 开始保洁
    handleBj(e){
      const {u} = e.currentTarget.dataset
      axios.post("/app/modifyRoomStatus",{
        id:u.ID,
        status:u.status === 5 ? 1 :5
      }).then(res=>{
        this.getList();
      })
      
      const obj = {
        currentTarget:{
          dataset:{
            u:0
          }
        }
      }
      if(u.dm!==0){
        obj.currentTarget.dataset.u=u.dm;
        if(u.status === 5){
          // 点击结束保洁
          this.handleDevice(obj,"close")
        }else{
          this.handleDevice(obj)
        }
      }
      if(u.dy!==0){
        obj.currentTarget.dataset.u=u.dy
        if(u.status === 5){
          // 点击结束保洁
          this.handleDevice(obj,"close")
        }else{
          this.handleDevice(obj)
        }
      }
      if(u.dm===0 && u.fjm!==0){
        obj.currentTarget.dataset.u=u.fjm
        if(u.status === 5){
          // 点击结束保洁
        }else{
          this.handleDevice(obj)
        }
      }
    },
    toOrder(e){
      const {u} = e.currentTarget.dataset;
      wx.navigateTo({
        url: '/pages/room/yuding/index?id='+u.ID+"&roomName="+u.name,
      })
    },
    handleDevice(e,type="open"){
      const {u,t} = e.currentTarget.dataset;
      axios.post("/app/handleDevice",{
        type:t||type,
        deviceId:u,
        remote:true
      }).then(res=>{
        if(res.msg){
          wx.showToast({
            title: res.msg,
            icon:"success"
          })
        }
        return
        if(res.deviceId){
          toOpenDoor(res.deviceId)
        }else{
          if(res.msg){
            wx.showToast({
              title: res.msg,
              icon:"success"
            })
          }
        }
      })
    }
  },
  lifetimes:{

  }
})